package com.nuorui.module.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuorui.common.core.code.UserErrorCode;
import com.nuorui.common.core.domain.ResponseDTO;
import com.nuorui.common.utils.SmartBeanUtil;
import com.nuorui.module.system.mapper.RoleMenuMapper;
import com.nuorui.module.system.mapper.RoleUserMapper;
import com.nuorui.module.system.domain.form.RoleForm;
import com.nuorui.module.system.domain.entity.RoleEntity;
import com.nuorui.module.system.mapper.RoleMapper;
import com.nuorui.module.system.service.RoleService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 角色管理
 *
 * @author: Fred
 * @email 453086@qq.com
 * @create: 2021-10-22 10:42
 */
@Service
@AllArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleEntity> implements RoleService {

    private final RoleUserMapper roleUserMapper;
    private final RoleMenuMapper roleMenuMapper;

    /**
     * 新增添加角色
     */
    @Override
    public ResponseDTO saveRole(RoleForm roleDTO) {
        RoleEntity existRoleEntity = baseMapper.getByRoleName(roleDTO.getRoleName());
        if (null != existRoleEntity) {
            return ResponseDTO.userErrorParam("角色名称重复");
        }
        existRoleEntity = baseMapper.getByRoleCode(roleDTO.getRoleCode());
        if (null != existRoleEntity) {
            return ResponseDTO.userErrorParam("角色编码重复，重复的角色为：" + existRoleEntity.getRoleName());
        }
        RoleEntity entity = SmartBeanUtil.copy(roleDTO, RoleEntity.class);
        baseMapper.insert(entity);

        return ResponseDTO.ok();
    }

    /**
     * 更新角色
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO updateRole(Long roleId, RoleForm roleDTO) {
        if (null == baseMapper.selectById(roleId)) {
            return ResponseDTO.error(UserErrorCode.DATA_NOT_EXIST);
        }

        RoleEntity existRoleEntity = baseMapper.getByRoleName(roleDTO.getRoleName());
        if (null != existRoleEntity && !existRoleEntity.getRoleId().equals(roleId)) {
            return ResponseDTO.userErrorParam("角色名称重复");
        }

        existRoleEntity = baseMapper.getByRoleCode(roleDTO.getRoleCode());
        if (null != existRoleEntity && !existRoleEntity.getRoleId().equals(roleId)) {
            return ResponseDTO.userErrorParam("角色编码重复，重复的角色为：" + existRoleEntity.getRoleName());
        }

        RoleEntity entity = SmartBeanUtil.copy(roleDTO, RoleEntity.class);
        baseMapper.updateById(entity);

        return ResponseDTO.ok();
    }

    /**
     * 根据角色id 删除
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO removeRole(Long roleId) {
        RoleEntity roleEntity = baseMapper.selectById(roleId);
        if (null == roleEntity) {
            return ResponseDTO.error(UserErrorCode.DATA_NOT_EXIST);
        }
        // 当没有员工绑定这个角色时才可以删除
        Integer exists = roleUserMapper.existsByRoleId(roleId);
        if (exists != null) {
            return ResponseDTO.error(UserErrorCode.ALREADY_EXIST, "该角色下存在员工，无法删除");
        }
        baseMapper.deleteById(roleId);
        roleMenuMapper.deleteByRoleId(roleId);
        roleUserMapper.deleteByRoleId(roleId);

        return ResponseDTO.ok();
    }
}